package com.source.lightfowl.basedata.service.impl;

import com.source.lightfowl.VO.Barcode_GrantVO;
import com.source.lightfowl.VO.Barcode_RecoveryVO;
import com.source.lightfowl.basedata.dao.*;
import com.source.lightfowl.basedata.entity.*;
import com.source.lightfowl.basedata.service.BarcodeService;
import com.source.lightfowl.basedata.service.StallsService;
import com.source.lightfowl.person.dao.PersonDao;
import com.source.lightfowl.person.entity.Person;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class BarcodeServiceImpl implements BarcodeService {
    @Autowired
    private BarcodeGrantDao barcodeGrantDao;
    @Autowired
    private BarcodeRecoveryDao barcodeRecoveryDao;
    @Autowired
    private BarcodeMachineGrantDao barcodeMachineGrantDao;
    @Autowired
    private BarcodeMachineRecoveryDao barcodeMachineRecoveryDao;
    @Autowired
    private PersonDao personDao;

    @Autowired
    private StallsDao stallsDao;
    @Autowired
    private StallsService stallsService;

    @Override
    public void saveBarcode_Grant(Barcode_Grant barcode_grant) {
        barcodeGrantDao.save(barcode_grant);
    }

    @Override
    public List<Barcode_GrantVO> findBarcode_Grant() {
        List<Barcode_Grant> list = barcodeGrantDao.findAll();

        List<Barcode_GrantVO> collect = list.stream().map(s -> {
            Barcode_GrantVO vo = new Barcode_GrantVO();
            BeanUtils.copyProperties(s, vo);

            //Person user = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getGrantPerson()).longValue()));
            Person user = personDao.findVO(s.getGrantPerson());
            //
            if (user != null) {
                vo.setDeliverName(user.getLoginName());
            }
            //Person user1 = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getReceivePerson()).longValue()));
            Person user1 = personDao.findVO(s.getReceivePerson());
            //
            if (user1 != null) {
                vo.setGetName(user1.getLoginName());
            }


            //档口名赋值
            String stallsId = s.getStallsId();
            Integer integer = Integer.valueOf(stallsId);
            Optional<Stalls> optional = stallsDao.findById(integer);
            if (optional.isPresent()) {
                vo.setStallsName(optional.get().getStallsName());
            }

            //创建者
            Person creat = personDao.findVO(s.getCreateBy());
            //
            if (creat != null) {
                vo.setCreateName(creat.getLoginName());
            }
            return vo;
        }).collect(Collectors.toList());


        return collect;
    }

    @Override
    public List<Barcode_GrantVO> findPageGrant(int page, int size) {
        Page<Barcode_Grant> list = barcodeGrantDao.findAll(PageRequest.of(page - 1, size));

        List<Barcode_GrantVO> collect = list.stream().map(s -> {
            Barcode_GrantVO vo = new Barcode_GrantVO();
            BeanUtils.copyProperties(s, vo);

            //Person user = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getGrantPerson()).longValue()));
            Person user = personDao.findVO(s.getGrantPerson());
            //
            if (user != null) {
                vo.setDeliverName(user.getLoginName());
            }
            //Person user1 = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getReceivePerson()).longValue()));
            Person user1 = personDao.findVO(s.getReceivePerson());
            //
            if (user1 != null) {
                vo.setGetName(user1.getLoginName());
            }

            //档口名


            return vo;
        }).collect(Collectors.toList());


        return collect;
    }

    @Override
    public void saveBarcode_Recovery(Barcode_Recovery barcode_recovery) {
        /*
        7.8 号 调用添加接口 发现出现错误 createDate没有初始值 添加下面代码之后还是出现相同的报错
        所以注释掉下面的代码 等后面再修改吧
        if (barcode_recovery.getCreateDate() == null){
            barcode_recovery.setCreateDate(new Date());
        }*/
        /*if(barcode_recovery.getId() == null){
           barcode_recovery.setCreateDate(new Date());
           barcode_recovery.setUpdateDate(new Date());
        }
*/
        barcodeRecoveryDao.save(barcode_recovery);
    }

    @Override
    public List<Barcode_Recovery> findBarcode_Recovery() {
        return barcodeRecoveryDao.findAll();
    }

    @Override
    public Page<Barcode_Recovery> findPage(int page, int size) {
        return barcodeRecoveryDao.findAll(PageRequest.of(page - 1, size));
    }

    /*
        @Override
        public void saveBarcode_Machine_Grant(Barcode_Machine_Grant barcode_machine_grant) {
            barcodeMachineGrantDao.save(barcode_machine_grant);
        }

        @Override
        public List<Barcode_Machine_Grant> findBarcode_Machine_Grant() {
            return barcodeMachineGrantDao.findAll();
        }

        @Override
        public void saveBarcode_Machine_Recovery(Barcode_Machine_Recovery barcode_machine_recovery) {
            barcodeMachineRecoveryDao.save(barcode_machine_recovery);
        }

        @Override
        public List<Barcode_Machine_Recovery> findBarcode_Machine_Recovery() {
            return barcodeMachineRecoveryDao.findAll();
        }
    */
    @Override
    public void updateBarcode_Grant(Barcode_Grant barcode_grant) {
        barcodeGrantDao.save(barcode_grant);
    }

    @Override
    public void updateBarcodeRecovery(Barcode_Recovery barcode_recovery) {
        barcodeRecoveryDao.save(barcode_recovery);
    }

    /*
        @Override
        public void updateBarcodeMachineGrant(Barcode_Machine_Grant barcode_machine_grant) {
            barcodeMachineGrantDao.save(barcode_machine_grant);
        }

        @Override
        public void updateBarcodeMachineRecovery(Barcode_Machine_Recovery barcode_machine_recovery) {
            barcodeMachineRecoveryDao.save(barcode_machine_recovery);
        }
    */
    @Override
    public void deleteBarcode_Grant(String id) {
        barcodeGrantDao.deleteById(id);
    }

    @Override
    public void deleteBarcode_Recovery(String id) {
        barcodeRecoveryDao.deleteById(id);
    }

    /*
        @Override
        public void deleteBarcodeMachineGrant(String id) {
            barcodeMachineGrantDao.deleteById(id);
        }

        @Override
        public void deleteBarcodeMachineRecovery(String id) {
            barcodeMachineRecoveryDao.deleteById(id);
        }
    */
    @Override
    public Barcode_GrantVO findBarcode_GrantById(String id) {

        Optional<Barcode_Grant> optional = barcodeGrantDao.findById(id);
        if (optional.isPresent()) {
            Barcode_Grant s = optional.get();
            Barcode_GrantVO vo = new Barcode_GrantVO();
            BeanUtils.copyProperties(s, vo);
            //档口名赋值
            String stallsId = s.getStallsId();
            Integer integer = Integer.valueOf(stallsId);
            Optional<Stalls> optional2 = stallsDao.findById(integer);
            if (optional.isPresent()) {
                vo.setStallsName(optional2.get().getStallsName());
            }

            Person user = personDao.findVO(s.getGrantPerson());
            //
            if (user != null) {
                vo.setDeliverName(user.getLoginName());
            }
            //Person user1 = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getReceivePerson()).longValue()));
            Person user1 = personDao.findVO(s.getReceivePerson());
            //
            if (user1 != null) {
                vo.setGetName(user1.getLoginName());
            }

            return vo;
        }


        return new Barcode_GrantVO();

    }

    @Override
    public Barcode_RecoveryVO findBarcode_RecoveryById(String id) {
        Optional<Barcode_Recovery> optional = barcodeRecoveryDao.findById(id);
        if (optional.isPresent()) {
            Barcode_Recovery s = optional.get();
            Barcode_RecoveryVO vo = new Barcode_RecoveryVO();
            BeanUtils.copyProperties(s, vo);

            Person user = personDao.findVO(s.getRecoveryPerson());
            if (user != null) {
                vo.setRecyclingName(user.getLoginName());
            }

            Person user1 = personDao.findVO(s.getSendbackPerson());
            //
            if (user1 != null) {
                vo.setSendName(user1.getLoginName());
            }

            //档口名
            String stallsId = s.getStallsId();
            Optional<Stalls> optiona5 = stallsService.findById(Integer.valueOf(stallsId));
            if (optional.isPresent()) {
                Stalls ss = optiona5.get();
                vo.setStallsName(ss.getStallsName());
            }

            return vo;
        }

        return new Barcode_RecoveryVO();
    }

    @Override
    public Barcode_Machine_Grant findBarcodeMachineGrantById(String id) {
        return barcodeMachineGrantDao.findById(id).get();
    }

    @Override
    public Barcode_Machine_Recovery findBarcode_Machine_RecoveryById(String id) {
        return barcodeMachineRecoveryDao.findById(id).get();
    }

    @Override
    public int getGrantCountByTime(String fromTime, String toTime) {
        return barcodeGrantDao.getGrantCountByTime(fromTime,toTime);
    }


}
